//
//  BasicSetViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/2/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//
    
import Foundation

class BasicSetViewController: BaseTRTCViewController {
    
    var dataList:[(String,Bool)] {
        let sd = Td.sh().cloud.disSd
        let brg = Td.sh().cloud.disBararage
        var titles:[(String,Bool)] = [("禁止共享",sd),("禁止弹幕",brg)]
        if Td.sh().scene == .live {
            let link = Td.sh().cloud.disLink
            titles.append(("禁止连麦",link))
        }
        titles.append(("锁定会议",false))
        titles.append(("屏蔽弹幕",false))
        return titles
    }
    var subbuttoms:[SettingBasic] = []
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSet_BasicWidth, kSet_BasicHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        //let defaultT = String(Td.sh().usr.max)
    }
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_gray(255).cgColor
        
        self.view.addSubview(content)
        content.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(480)
            make.top.equalToSuperview().offset(40)
            make.height.equalToSuperview()
        }
        self.subbuttoms = self.initButtoms(inView: content,btnTitles: self.dataList)
    }
    
    lazy var content: NSView = {
        let view = NSView()
        return view
    }()
    
    func initButtoms(inView:NSView,btnTitles:[(String,Bool)] ) -> [SettingBasic] {
        let h =  2
        let v =  Int(ceilf(Float(btnTitles.count)/Float(2)))
        var buttoms:[SettingBasic] = []
        for i in 0..<v {
            for j in 0..<h{
                let index = i*h+j
                if index >= btnTitles.count {
                    return buttoms
                }
                
                let title = btnTitles[index].0
                let isOn = btnTitles[index].1
                let buttom = SettingBasic.init { (view) in
                    self.subbuttomsAction(sender: view)
                }
                buttom.title.stringValue = isOn ? "解除"+title:title
                buttom.tag = index
                buttom.isSelected = isOn
                buttom.canSelected = Td.sh().isJoin == false
                inView.addSubview(buttom)
                buttoms.append(buttom)
                
                buttom.snp.makeConstraints { (make) in
                    make.width.equalTo(220)
                    make.height.equalTo(30)
                    if j == 0 {
                        make.left.equalTo(0)
                    }else {
                        make.left.equalTo(buttoms[j-1].snp.right).offset(30)
                    }
                    if i == 0 {
                        make.top.equalTo(0)
                    }else {
                        make.top.equalTo(buttoms[i-1].snp.bottom).offset(15)
                    }
                    
                }
            }
        }
        return buttoms
        
    }
    
    @objc func subbuttomsAction(sender:SettingBasic) {
        if Td.sh().isJoin == true {
            show_msg("只有主持人才能修改")
            return
        }
        
        let isSelected = self.dataList[sender.tag].1
        let title = self.dataList[sender.tag].0
        guard let roomid = Td.sh().params.roomId  else { return }
        sender.title.stringValue = "..."
        switch sender.tag {
        case 0:
            NetEngine.respose_Isshare(ownid: roomid, isOk: isSelected, success: {
                IMcmd.shared.sd(allow: isSelected)
                Td.sh().cloud.disSd = !isSelected
                sender.title.stringValue = !isSelected ? "解除禁止共享":"禁止共享"
                sender.isSelected = !isSelected
            }) {
                sender.title.stringValue = title
                show_msg("设置禁止共享失败")
            }
        case 1:
            NetEngine.respose_Issilent(ownid: roomid, isOk: isSelected, success: {
                IMcmd.shared.disDanmu(isok: !isSelected)
                Td.sh().cloud.disBararage = !isSelected
                sender.title.stringValue = !isSelected ? "解除禁止弹幕":"禁止弹幕"
                sender.isSelected = !isSelected
            }) {
                sender.title.stringValue = title
                show_msg("设置禁止弹幕失败")
            }
            
        case 2:
            NetEngine.respose_Ismike(ownid: roomid, isOk: isSelected, success: {
                IMcmd.shared.notAllowToRole(isok: !isSelected)
                Td.sh().cloud.disLink = !isSelected
                sender.title.stringValue = !isSelected ? "解除禁止连麦":"禁止连麦"
                sender.isSelected = !isSelected
            }) {
                sender.title.stringValue = title
                show_msg("设置禁止连麦失败")
            }
        default:
            break
        }
    }
    
    
}
